package cn.edu.scau.cmi.yejiancong.composit;

import javafx.fxml.FXML;

import javafx.scene.control.Button;

import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.TreeItem;

import java.util.ArrayList;

import com.sun.javafx.scene.control.skin.VirtualFlow.ArrayLinkedList;

import javafx.event.ActionEvent;

import javafx.scene.control.Label;

import javafx.scene.control.RadioButton;

import javafx.scene.control.TreeView;
import javafx.scene.layout.StackPane;

public class GUIController {
	@FXML
	private Button create;
	@FXML
	private Label info;
	@FXML
	private TreeView treeview=new TreeView<String>();
	@FXML
	private TextField name;
	@FXML
	private Button check;
	@FXML
	private RadioButton deptF;
	@FXML
	private RadioButton deptB;
	@FXML
	private Button delete;
	
	
	final ToggleGroup tg=new ToggleGroup();
	private Composite cpComposite;
	private TreeItem<String> deptment=new TreeItem<String>("root");
	private TreeItem<String> backdept=new TreeItem<String>("backdeptment");
	private TreeItem<String> frondept=new TreeItem<>("frondeptment");
	private int id=0;
	public GUIController() {
		cpComposite=new Composite();
		deptB=new RadioButton();
		deptF=new RadioButton();
		deptB.setToggleGroup(tg);
		deptF.setToggleGroup(tg);
		treeview.setRoot(deptment);
		deptment.getChildren().add(backdept);
		deptment.getChildren().add(frondept);
		// TODO Auto-generated constructor stub
	}
	
	// Event Listener on Button[#create].onAction
	@FXML
	public void create(ActionEvent event) {
		int flag=-1;
		if(deptB.isSelected()) flag=0;
		if(deptF.isSelected()) flag=1;
		if(flag==0) {
			company cp=new backE(name.getText(),id);
			id++;
			cpComposite.add(cp);
			TreeItem<String> t1=new TreeItem<>(cp.toString());
			backdept.getChildren().add(t1);
			
		}
		else if(flag==1) {
			company cp=new frontE(name.getText(),id);
			id++;
			cpComposite.add(cp);
			TreeItem<String> t1=new TreeItem<>(cp.toString());
			frondept.getChildren().add(t1);
			
		}treeview.setRoot(deptment);
		
		
	}
	// Event Listener on Button[#check].onAction
	@FXML
	public void showlist(ActionEvent event) {
		String meString=treeview.getSelectionModel().getSelectedItem().toString().replace("TreeItem [ value: ","");
		
		info.setText(meString.replace("]", ""));
		
	}
	
	public void del() {
		ArrayList<company> cps=cpComposite.getList();
		String mes=treeview.getSelectionModel().getSelectedItem().toString().replace("TreeItem [ value: ","");
		String meString=mes.replace(" ]", "");
		String dmes="";
		for(int i=0;i<cpComposite.getList().size();i++) {
			
			if(cpComposite.getChild(i).toString().equals(meString)) { 
				company jCompany=(company)cpComposite.getChild(i);
			cpComposite.remove(cpComposite.getChild(i));
			dmes=jCompany.getDept();
			
			System.out.println("delete");
			
			}
		}
		if(dmes.equals("frontE")) frondept.getChildren().remove(treeview.getSelectionModel().getSelectedItem());
		else if(dmes.equals("backE")) backdept.getChildren().remove(treeview.getSelectionModel().getSelectedItem());
		
       
		
        treeview.setRoot(deptment);
	}
	
	
	
	
}
